package com.xiaoyun.controller.center;

import com.xiaoyun.bo.center.OrderItemsCommentBO;
import com.xiaoyun.constant.Const;
import com.xiaoyun.controller.BaseController;
import com.xiaoyun.enums.YesOrNo;
import com.xiaoyun.pojo.OrderItems;
import com.xiaoyun.pojo.Orders;
import com.xiaoyun.service.center.MyCommentService;
import com.xiaoyun.utils.PagedGridResult;
import com.xiaoyun.utils.XYJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("用户中心评论模块")
@RestController
@RequiredArgsConstructor
@RequestMapping("mycomments")
public class MyCommentController extends BaseController {

    private final MyCommentService myCommentService;

    @ApiOperation("查询订单列表")
    @GetMapping("pending")
    public XYJSONResult pending
            (@RequestParam String userId,
             @RequestParam String orderId) {
        XYJSONResult checkUserOrder = checkUserOrder(userId, orderId);
        if (checkUserOrder.getStatus() != HttpStatus.OK.value()) {
            return checkUserOrder;
        }
        Orders myOrder = (Orders) checkUserOrder.getData();
        if (myOrder.getIsComment() == YesOrNo.YES.type) {
            return XYJSONResult.errorMsg("该笔订单已经评价");
        }
        List<OrderItems> list = myCommentService.queryPendingComment(orderId);
        return XYJSONResult.ok(list);
    }

    @ApiOperation("保存评论列表")
    @GetMapping("saveList")
    public XYJSONResult saveList
            (@RequestParam String userId,
             @RequestParam String orderId,
             @RequestBody List<OrderItemsCommentBO> commentList) {

        XYJSONResult checkUserOrder = checkUserOrder(userId, orderId);
        if (checkUserOrder.getStatus() != HttpStatus.OK.value()) {
            return checkUserOrder;
        }
        if (commentList == null || commentList.isEmpty() || commentList.size() == 0) {
            return XYJSONResult.errorMsg("评论内容为空");
        }
        myCommentService.saveComments(orderId, userId, commentList);

        return XYJSONResult.ok();
    }


    @ApiOperation("查询我的评价")
    @PostMapping("/query")
    public XYJSONResult query(
            @RequestParam String userId,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = Const.COMMENT_PAGE_SIZE) Integer pageSize) {
        if (StringUtils.isBlank(userId)) {
            return XYJSONResult.errorMsg("");
        }
        PagedGridResult gridResult = myCommentService.queryMyComments(userId, page, pageSize);
        return XYJSONResult.ok(gridResult);
    }


}
